package com.ztsoft.user.service.project.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ztsoft.user.mapper.project.DemandUnitMapper;
import com.ztsoft.user.pojo.entity.project.DemandUnit;
import com.ztsoft.user.pojo.vo.project.ProjectMeta;
import com.ztsoft.user.service.project.DemandUnitService;
import com.ztsoft.user.utils.Result;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class DemandUnitServiceImpl extends ServiceImpl<DemandUnitMapper, DemandUnit> implements DemandUnitService {

    @Override
    public Result<Object> updateDemandUnit(DemandUnit demandUnit) {
        boolean isSuccess = this.updateById(demandUnit);
        if (isSuccess) {
            return Result.success();
        }
        return Result.fail();
    }

    @Override
    public Result<Object> addDemandUnit(DemandUnit demandUnit) {
        boolean isSuccess = this.save(demandUnit);
        if (isSuccess) {
            return Result.success();
        }
        return Result.fail();
    }

    @Override
    public Result<Object> deleteDemandUnit(String id) {
        boolean isSuccess = this.removeById(id);
        if (isSuccess) {
            return Result.success();
        }
        return Result.fail();
    }

    @Override
    public Result<Object> getDemandUnitName() {
        List<DemandUnit> demandUnitList = list();
        List<ProjectMeta> demandUnitNameList = new ArrayList<>();
        demandUnitList.forEach(demandUnit -> demandUnitNameList.add(new ProjectMeta(demandUnit.getId(), demandUnit.getDemandUnitName())));
        return Result.success(demandUnitNameList);
    }
}
